package com.hmyd.hmyd_android.ui;

import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.SinaDetail;
import com.hmyd.hmyd_android.dto.User;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.HomeSeekArc;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 安全中心
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_security_center)
public class SecurityCenterActivity extends BasicActivity {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	// 安全等级
	@ViewInject(R.id.seek_bar)
	private HomeSeekArc seek_bar;
	@ViewInject(R.id.level_label)
	private TextView level_label;
	// 登录密码
	@ViewInject(R.id.changeSecurityCode)
	private View changeSecurityCode;
	// 手势密码
	@ViewInject(R.id.gesture_setting)
	private ImageView mGestureSetting;
	// 实名认证
	@ViewInject(R.id.authRealName)
	private View authRealName;
	@ViewInject(R.id.authRealNameLabel)
	private TextView authRealNameLabel;
	@ViewInject(R.id.authRealNameLabelSuccess)
	private ImageView authRealNameLabelSuccess;
	@ViewInject(R.id.authRealNameTitle)
	private TextView authRealNameTitle;
	// 手机认证
	@ViewInject(R.id.telName)
	private TextView telName;
	// 邮箱认证
	@ViewInject(R.id.sendMailVerify)
	private View sendMailVerify;
	@ViewInject(R.id.sendMailVerifyLabel)
	private TextView sendMailVerifyLabel;
	// 邮箱名
	@ViewInject(R.id.mailName)
	private TextView mailName;
	// 安全问题
	@ViewInject(R.id.securityQuestion)
	private View securityQuestion;
	@ViewInject(R.id.securityQuestionLabel)
	private TextView securityQuestionLabel;
	// 账户保护
	@ViewInject(R.id.accountprotection)
	private ImageView accountprotection;
	// 冻结账号
	@ViewInject(R.id.frozenaccount)
	private TextView frozenaccount;
	// 解冻账号
	// @ViewInject(R.id.unfrozenaccount)
	// private TextView unfrozenaccount;

	private final int GET_MY_INFO = 0;
	private final int OPEN = 1;// 开启设备锁
	private final int CLOSE = 2;// 关闭设备锁

	private User user;

	@OnClick({ R.id.changeSecurityCode, R.id.gesture_setting,
			R.id.authRealName, R.id.telName, R.id.sendMailVerify, R.id.securityQuestion,
			R.id.accountprotection, R.id.frozenaccount, R.id.accountprotection })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.changeSecurityCode:
			startActivity(new Intent(this, ChangePasswordActivity.class));
			break;
		/*case R.id.changePassword:
			if (AppBasic.isSetSecurityCode) {
				startActivity(new Intent(this, TradingSettingPasswordActivity.class));
			} else {
				startActivity(TradingPasswordActivity.getIntent(this,
						getResources().getString(R.string.setting_trading_password)));
			}
			break;*/
		case R.id.gesture_setting:
			if (!AppBasic.getInstance().getLockPatternUtils().savedPatternExists()) {
				startActivity(new Intent(this, CreateGesturePasswordActivity.class));
			} else {
				mGestureSetting.setImageResource(R.drawable.an_kg_off);
				AppBasic.getInstance().getLockPatternUtils().clearLock();
				MyToast.showHMToast(this, true, "手势解锁已取消");
			}
			break;
		case R.id.authRealName:
			if (!TextUtils.isEmpty(AppBasic.certNo) && !TextUtils.isEmpty(AppBasic.realName)) {
				MyToast.showHMToast(SecurityCenterActivity.this, false, "你已经实名认证过了");
			} else {
				startActivity(new Intent(this, CreateAccountActivity.class));
			}

			break;
		case R.id.telName:
			// Toast.makeText(getApplicationContext(), "手机认证",
			// Toast.LENGTH_SHORT).show();
			break;
		case R.id.sendMailVerify:
			if (TextUtils.isEmpty(AppBasic.email)) {
				startActivity(new Intent(this, MailVerifyActivity.class));
			} else if (!AppBasic.isVerifiedEmail) {
				startActivity(new Intent(this, MailVerifyActivity.class));
			} else {
				startActivity(new Intent(this, MailUpdateActivity.class));
			}
			break;
		case R.id.securityQuestion:
			if (AppBasic.isSetSecurityQuestion) {
				startActivity(new Intent(SecurityCenterActivity.this,
						ChangeSecurityIssuesActivity.class));
			} else {
				startActivity(new Intent(SecurityCenterActivity.this, SecurityIssuesActivity.class));
			}
			break;
		case R.id.accountprotection:
			if (accountprotection.getTag().equals("on")) {
				accountprotection.setTag("off");
				accountprotection.setImageResource(R.drawable.an_kg_off);
				closeProtect();
			} else if (accountprotection.getTag().equals("off")) {
				accountprotection.setTag("on");
				accountprotection.setImageResource(R.drawable.an_kg_on);
				openProtect();
			}

			break;
		case R.id.frozenaccount:
			startActivity(new Intent(SecurityCenterActivity.this, ForzenAccountActivity.class));

			break;
		default:
			break;
		}

	}

	/**
	 * 开启设备锁
	 */
	private void openProtect() {
		RequestParams params = new RequestParams();
		if (String.valueOf(accountprotection.getTag()).equals("on")) {
			params.addBodyParameter("flag", "true");
		}

		onRequest(OPEN, UrlConstant.SWITCHDEVICECHAIN, HttpMethod.POST, params);

	}

	/**
	 * 关闭设备锁
	 */
	private void closeProtect() {

		// RequestParams params = new RequestParams();
		// params.addBodyParameter("flag",
		// String.valueOf(accountprotection.getTag()));
		// params.addBodyParameter("codeId", "");
		// params.addBodyParameter("verifyCode", "");
		// onRequest(CLOSE, UrlConstant.SWITCHDEVICECHAIN, HttpMethod.POST,
		// params);
		startActivity(new Intent(this, CloseDeviceProtectActivity.class));

	}

	@Override
	protected void onResume() {
		super.onResume();
		onRequest(GET_MY_INFO, UrlConstant.GETMYINFO, HttpMethod.POST, null);
		
		if (!AppBasic.getInstance().getLockPatternUtils().savedPatternExists()) {
			mGestureSetting.setImageResource(R.drawable.an_kg_off);
		}else{
			mGestureSetting.setImageResource(R.drawable.an_kg_on);
		}
	}

	@Override
	public void onPostSuccess(int postId, String result) {

		Gson gson = new Gson();
		switch (postId) {
		case GET_MY_INFO:
			java.lang.reflect.Type type = new TypeToken<User>() {
			}.getType();
			user = gson.fromJson(result, type);

			AppBasic.isCheckIned = user.isCheckIned;
			AppBasic.totalScore = user.score;
			AppBasic.certNo = user.certNo;
			AppBasic.realName = user.realName;
			AppBasic.avatar = user.avatar;
			AppBasic.username = user.username;
			AppBasic.nickname = user.nickname;
			AppBasic.urlToken = user.urlToken;
			AppBasic.securityLevel = user.securityLevel;
			AppBasic.isSetSecurityCode = user.isSetSecurityCode;
			AppBasic.tel = user.tel;
			AppBasic.email = user.email;
			AppBasic.isVerifiedEmail = user.isVerifiedEmail;
			AppBasic.isSetSecurityQuestion = user.isSetSecurityQuestion;

			initData();

			break;
		case OPEN:
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(result);
				MyToast.showHMToast(this, true, jsonObject.getString("info"));
			} catch (JSONException e) {
				e.printStackTrace();
			}

			break;
		default:
			break;
		}

	}

	private void initData() {

		if (!TextUtils.isEmpty(AppBasic.securityLevel)) {
			if ("Low".equals(AppBasic.securityLevel)) {
				level_label.setText("低");
				level_label.setTextColor(getResources().getColor(R.color.home_rate_color));

				seek_bar.setProgress(33, false);
				seek_bar.setProgressColor(getResources().getColor(R.color.home_rate_color));
				seek_bar.setThumbDrawable(R.drawable.ind_to_dian);
			} else if ("Medium".equals(AppBasic.securityLevel)) {
				level_label.setText("中");
				level_label.setTextColor(getResources().getColor(R.color.home_shf_text));

				seek_bar.setProgress(66, false);
				seek_bar.setProgressColor(getResources().getColor(R.color.home_shf_text));
				seek_bar.setThumbDrawable(R.drawable.ind_fe_dian);
			} else if ("High".equals(AppBasic.securityLevel)) {
				level_label.setText("高");
				level_label.setTextColor(getResources().getColor(R.color.home_phb_text));

				seek_bar.setProgress(100, false);
				seek_bar.setProgressColor(getResources().getColor(R.color.home_phb_text));
				seek_bar.setThumbDrawable(R.drawable.ind_ba_dian);
			}
		}

		if (!TextUtils.isEmpty(AppBasic.certNo) && !TextUtils.isEmpty(AppBasic.realName)) {
			authRealNameLabel.setVisibility(View.GONE);
			authRealNameLabelSuccess.setVisibility(View.VISIBLE);
			authRealNameTitle.setCompoundDrawablesWithIntrinsicBounds(
					getResources().getDrawable(R.drawable.an_icon_shi), null, null, null);
			authRealNameTitle.setText("实名认证(" + AppBasic.realName + ")");
		} else {
			authRealNameLabel.setVisibility(View.VISIBLE);
			authRealNameLabelSuccess.setVisibility(View.GONE);
			authRealNameTitle.setCompoundDrawablesWithIntrinsicBounds(
					getResources().getDrawable(R.drawable.an_icon_shi), null, getResources()
							.getDrawable(R.drawable.xi_icon_jin), null);
		}

		if (!TextUtils.isEmpty(AppBasic.tel)) {
			telName.setText("手机认证(" + AppBasic.tel + ")");
		}

		if (TextUtils.isEmpty(AppBasic.email)) {
			sendMailVerifyLabel.setText("未设置");
			sendMailVerifyLabel.setTextColor(getResources().getColor(R.color.home_rate_color));

		} else if (!AppBasic.isVerifiedEmail) {
			sendMailVerifyLabel.setText("未激活");
			sendMailVerifyLabel.setTextColor(getResources().getColor(R.color.home_rate_color));
			mailName.setText("邮箱认证(" + AppBasic.email + ")");
		} else {
			sendMailVerifyLabel.setText("已激活");
			sendMailVerifyLabel
					.setTextColor(getResources().getColor(R.color.investment_detail_tip));
			mailName.setText("邮箱认证(" + AppBasic.email + ")");
		}

		if (AppBasic.isSetSecurityQuestion) {
			securityQuestionLabel.setText("修改");
			securityQuestionLabel.setTextColor(getResources().getColor(
					R.color.investment_detail_tip));
		} else {
			securityQuestionLabel.setText("未设置");
			securityQuestionLabel.setTextColor(getResources().getColor(R.color.home_rate_color));
		}

		if (user.switchDeviceChain) {
			accountprotection.setTag("on");
			accountprotection.setImageResource(R.drawable.an_kg_on);
		} else {
			accountprotection.setTag("off");
			accountprotection.setImageResource(R.drawable.an_kg_off);
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_title_txt.setText("安全中心");
		home_title.setOnTouchListener(touch);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

}
